﻿using System;
using Epm.AppLogic.Framework;
using msBsc.Xsd.DataTds;
using System.Data;

namespace msBsc.AppLogic.DataAlc
{
    public class CBizShpOrderForm : CustBizObject
    {
        private CDmShpOrderForm m_CDmShpOrderForm = new CDmShpOrderForm();
        private CDmShpOrderForm oDmShpOrderForm
        {
            get { return m_CDmShpOrderForm; }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        public CBizShpOrderForm()
            : base()
        {
            this.DataObj = new CDbAccess();
            this.DataObj.Init(this.m_CDmShpOrderForm.SHP_ORDER_FORM);
            this.TableName = this.DataObj.TableName;
            this.DataObj.KeyField = this.DataObj.Fields["ORDER_FORM_ID"];
            //this.DataObj.IdGroup = "RID";
        }

        protected override bool BeforeRowDeletedR(DataRow drw)
        {



            return true;
        }


        /// <summary>
        /// 新增记录前的处理
        /// </summary>
        /// <param name="drw">新增数据行</param>
        /// <returns>true/false</returns>
        /// <remarks>
        /// 生成ID值
        /// </remarks>
        protected override bool BeforeRowAdded(DataRow drw)
        {
            if (drw[this.KeyFieldName].ToString().Length < 10)
            {
                drw[this.KeyFieldName] = this.DataObj.NewID();
            }
            drw["SELLER_DEL"] = false;
            drw["BUYER_DEL"] = false;
            drw["BUYER_RATE_SID"] = "";
            if (drw["USER_NME"].ToString() == drw["COMM_USER_NME"].ToString())
            {
                throw new Exception("自己不能购买自己的商品！");
            }
            if (drw["RCV_SCORE_QTY"].ToString() == string.Empty)
            {
                drw["RCV_SCORE_QTY"] = 0;
            }
            if (drw["PAID_SCORE_QTY"].ToString() == string.Empty)
            {
                drw["PAID_SCORE_QTY"] = 0;
            }
            if (drw["PAID_SCORE_AMT"].ToString() == string.Empty)
            {
                drw["PAID_SCORE_AMT"] = 0M;
            }
            if (drw["PAID_AMT"].ToString() == string.Empty)
            {
                drw["PAID_AMT"] = 0M;
            }
            if (drw["ORDER_TYPE_CID"].ToString() == string.Empty)
            {
                drw["ORDER_TYPE_CID"] = "实物";//订单类型：实物，票卷，团购
            }

            //校验数据

            if ((int)drw["PAID_SCORE_QTY"] > 0)
            {
                using (msBsc.AppLogic.DataAlc.CBizShpCustomerInfo ouinfo = new CBizShpCustomerInfo())
                {
                    int iScoreSum = ouinfo.GetUserScoreAllowUseQty(drw["USER_NME"].ToString());
                    if (iScoreSum < (int)drw["PAID_SCORE_QTY"])
                    {
                        throw new Exception("当前用户可用积分太少【" + iScoreSum.ToString() + "点】，不够本次抵扣【" + drw["PAID_SCORE_QTY"].ToString() + "点】！");
                    }
                }
                drw["PAID_AMT"] = (decimal)drw["PAYABLE_AMT"] - (decimal)drw["PAID_SCORE_AMT"];
            }

            return true;
        }
        public string GetNewTradingNo()
        {
            return "03" + msBsc.SvRef.ApplicationConst.Const.GeneratelongId().ToString();
        }

        /// <summary>
        /// 记录修改前检验
        /// </summary>
        /// <param name="drw">当前记录</param>
        /// <returns>>校验合法，返回值为true,否则返回false</returns>
        /// <remarks>
        /// ID不允许修改,并把此修改操作记录到BSE_MODIFY_HISTORY表中
        /// </remarks>
        protected override bool BeforeRowModified(DataRow drw)
        {
            //ID不容许修改
            if (drw[this.KeyFieldName].ToString() != drw[this.KeyFieldName, DataRowVersion.Original].ToString())
            {
                Exception e = new Exception(this.TableName + "表的主键" + this.KeyFieldName + "字段不允许被修改");
                throw e;
            }
            if (drw["TRADE_STATUS_TXT"].ToString() != drw["TRADE_STATUS_TXT", DataRowVersion.Original].ToString())
            {
                if (drw["TRADE_STATUS_TXT"].ToString() == "买家已付款")
                {
                    drw["PAYMENT_DT"] = System.DateTime.Now; 
                }
                if (drw["TRADE_STATUS_TXT"].ToString() == "卖家已发货")
                {
                    drw["DELIVER_DT"] = System.DateTime.Now; 
                }
                //卷票交易发货后自动完成交易
                //订单类型：实物，票卷，团购
                if (drw["TRADE_STATUS_TXT"].ToString() == "卖家已发货")
                {
                    if (drw["ORDER_TYPE_CID"].ToString() == "票卷")
                    {
                        drw["ORDER_TYPE_CID"] = "交易成功";
                    }
                }
                if (drw["TRADE_STATUS_TXT"].ToString() == "交易成功")
                {
                    drw["AFFIRM_DT"] = System.DateTime.Now; 
                }
                if (drw["TRADE_STATUS_TXT"].ToString() == "交易关闭")
                {
                    drw["CLOSE_DT"] = System.DateTime.Now; 
                }
                if (drw["TRADE_STATUS_TXT"].ToString() == "交易成功")
                {
                    drw["BUYER_RATE_SID"] = "0";
                    drw["SELLER_RATE_SID"] = "0";
                }
            }
            if (drw["BUYER_RATE_SID"].ToString() != drw["BUYER_RATE_SID", DataRowVersion.Original].ToString())
            {
                if (drw["BUYER_RATE_SID"].ToString() == "1")
                {
                    drw["BUYER_RATE_DT1"] = DateTime.Now;
                }
            }
            if (drw["SELLER_RATE_SID"].ToString() != drw["SELLER_RATE_SID", DataRowVersion.Original].ToString())
            {
                if (drw["SELLER_RATE_SID"].ToString() == "1")
                {
                    drw["SELLER_RATE_DT1"] = DateTime.Now;
                }
            }
            if (drw["LOGISTICS_AMT"].ToString() != drw["LOGISTICS_AMT", DataRowVersion.Original].ToString())
            {
                drw["PAYABLE_AMT"] = ((decimal)drw["GOODS_AMT"] - (decimal)drw["DISCOUNT_AMT"]) + (decimal)drw["LOGISTICS_AMT"];
            }
            return true;
        }

        protected override bool AfterRowModified(DataRow drw)
        {
            if (drw["TRADE_STATUS_TXT"].ToString() != drw["TRADE_STATUS_TXT", DataRowVersion.Original].ToString())
            {
                //if ("等待买家付款" != drw["TRADE_STATUS_TXT", DataRowVersion.Original].ToString())
                //{
                    if (drw["TRADE_STATUS_TXT"].ToString() == "交易关闭")
                    {
                        using (CBizShpChargeTradingLog objlog = new CBizShpChargeTradingLog())
                        {
                            objlog.AddTradingLogForOrderReturn(drw);
                        }
                    }
                //}
                if (drw["TRADE_STATUS_TXT"].ToString() == "交易成功")
                {
                    //更新实际库存
                    this.DataObj.ExecuteBySQL(@"
    UPDATE B SET 
    GOODS_ACT_QTY=B.GOODS_ACT_QTY-A.GOODS_QTY
    FROM SHP_ORDER_FORM_LIST A
    LEFT OUTER JOIN SHP_GOODS_NORMS_PUBLIC B ON A.GOODS_ID=B.GOODS_ID AND A.PUB_NORMS_ID=B.PUB_NORMS_ID
    WHERE
    A.ORDER_FORM_ID='" + drw["ORDER_FORM_ID"].ToString() + @"'
");
                    if ((int)drw["RCV_SCORE_QTY"] > 0)
                    {
                        using (CBizShpScoreTradingLog otradingLog = new CBizShpScoreTradingLog())
                        {
                            otradingLog.AddTradingLogForCmpOrder(drw);
                        }
                    }
                    using (CBizShpChargeTradingLog objchglog = new CBizShpChargeTradingLog())
                    {
                        objchglog.AddTradingLogForOrderCmp(drw);
                    }

                    AddOrderRate(drw[this.KeyFieldName].ToString());
                    UpdateStatisticsInfo(drw[this.KeyFieldName].ToString());
                }
                if (drw["TRADE_STATUS_TXT"].ToString() == "交易关闭")
                {
                    //更新销售库存
                    this.DataObj.ExecuteBySQL(@"
    UPDATE B SET 
    GOODS_QTY=B.GOODS_QTY+A.GOODS_QTY
    FROM SHP_ORDER_FORM_LIST A
    LEFT OUTER JOIN SHP_GOODS_NORMS_PUBLIC B ON A.GOODS_ID=B.GOODS_ID AND A.PUB_NORMS_ID=B.PUB_NORMS_ID
    WHERE
    A.ORDER_FORM_ID='" + drw["ORDER_FORM_ID"].ToString() + @"'
");
                }
                //卷票交易付款后自动发验证码
                //订单类型：实物，票卷，团购
                if (drw["TRADE_STATUS_TXT"].ToString() == "买家已付款")
                {
                    if (drw["ORDER_TYPE_CID"].ToString() == "票卷")
                    {
                        SetGiftVolumeOrderGoodsCode(drw);
                    }
                }
            }
            if ( drw["BUYER_RATE_SID"].ToString() != drw["BUYER_RATE_SID", DataRowVersion.Original].ToString()
                ||  drw["SELLER_RATE_SID"].ToString() != drw["SELLER_RATE_SID", DataRowVersion.Original].ToString())
            {
                UpdateStatisticsInfo(drw[this.KeyFieldName].ToString());
            }
            return true;
        }


        protected override bool AfterRowAdded(DataRow drw)
        {
            //更新销售库存
            this.DataObj.ExecuteBySQL(@"
    UPDATE B SET 
    GOODS_QTY=B.GOODS_QTY-A.GOODS_QTY
    FROM SHP_ORDER_FORM_LIST A
    LEFT OUTER JOIN SHP_GOODS_NORMS_PUBLIC B ON A.GOODS_ID=B.GOODS_ID AND A.PUB_NORMS_ID=B.PUB_NORMS_ID
    WHERE
    A.ORDER_FORM_ID='" + drw["ORDER_FORM_ID"].ToString() + @"'
");

            //建立付款账单
            AddPayLedger(drw);
            return true;
        }

        private void AddPayLedger(DataRow drw)
        {
            if ((int)drw["PAID_SCORE_QTY"] > 0)
            {
                using (CBizShpScoreTradingLog oscoreLog = new CBizShpScoreTradingLog())
                {
                    oscoreLog.AddTradingLogForAddOrder(drw);
                }
            }

            if ((decimal)drw["PAID_AMT"] > 0M)
            {
                using (CBizShpChargeTradingLog oscoreLog = new CBizShpChargeTradingLog())
                {
                    oscoreLog.AddTradingLogForAddOrder(drw);
                }
            }
        }

        //卷票交易付款后自动发验证码
        private void SetGiftVolumeOrderGoodsCode(DataRow drw)
        {
            //DataSet ds = new CDmShpOrderForm();
            //DataRow drorder= ds.Tables[0].NewRow();
            //Epm.AppLogic.Framework.CUtil.CopyDataRow(drw, drorder);
            //ds.Tables[0].Rows.Add(drorder);
            //ds.AcceptChanges();
            //drorder["TRADE_STATUS_TXT"] = "";
            using (CBizShpOrderFormList objlist = new CBizShpOrderFormList())
            {
                DataSet dslist = objlist.SelectByOrderId(drw[this.KeyFieldName].ToString());
                for (int i = dslist.Tables[0].Rows.Count - 1; i >= 0; i--)
                {
                    if (dslist.Tables[0].Rows[0]["GOODS_CDKEY"].ToString() == string.Empty)
                    {
                        dslist.Tables[0].Rows[0]["GOODS_CDKEY"] = msBsc.SvRef.ApplicationConst.Const.GenerateStrId();
                    }
                }
                objlist.Update(dslist);
            }
        }


        public DataSet SelectByIds(string strIdS)
        {
            DataSet ds = this.DataObj.QueryBySQL("SELECT * FROM " + this.TableName + " WHERE  " + this.KeyFieldName + " IN  ('" + strIdS.Replace("'", "").Replace(",", "','") + "')");
            ds.Tables[0].TableName = this.TableName;
            return ds;
        }

        public DataSet SelectByNos(string strNoS)
        {
            DataSet ds = this.DataObj.QueryBySQL("SELECT * FROM " + this.TableName + " WHERE  ORDER_FORM_NO IN  ('" + strNoS.Replace("'", "").Replace(",", "','") + "') ORDER BY ORDER_FORM_NO ASC");
            ds.Tables[0].TableName = this.TableName;
            return ds;
        }

        public void AddOrderRate(string strOrderId)
        {
            CDbParmCollection parmCollection = new CDbParmCollection();
            parmCollection.Add(new CDbParm("strOrderId", ParameterDirection.Input, EDbType.String, 36, strOrderId));
            this.DataObj.ExecuteBySP("SHP_COPY_ORDER_LIST_ADD_ORDER_RATE", parmCollection);
        }

        /// <summary>
        /// 系统后台订单管理
        /// </summary>
        /// <param name="strOrderNo"></param>
        /// <param name="strUserNme"></param>
        /// <param name="strDealerUserNme"></param>
        /// <param name="strOrderDtS"></param>
        /// <param name="strOrderDtE"></param>
        /// <param name="strOrderSid"></param>
        /// <param name="strPayMode"></param>
        /// <returns></returns>
        public DataSet SelectOrderSysFinder(string strOrderNo, string strUserNme, string strDealerUserNme, string strOrderDtS, string strOrderDtE, string strOrderSid, string strPayMode)
        {
            CDbParmCollection parmCollection = new CDbParmCollection();
            parmCollection.Add(new CDbParm("strOrderNo", ParameterDirection.Input, EDbType.String, 36, strOrderNo));
            parmCollection.Add(new CDbParm("strUserNme", ParameterDirection.Input, EDbType.String, 36, strUserNme));
            parmCollection.Add(new CDbParm("strDealerUserNme", ParameterDirection.Input, EDbType.String, 36, strDealerUserNme));
            parmCollection.Add(new CDbParm("strOrderDtS", ParameterDirection.Input, EDbType.String, 36, strOrderDtS));
            parmCollection.Add(new CDbParm("strOrderDtE", ParameterDirection.Input, EDbType.String, 36, strOrderDtE));
            parmCollection.Add(new CDbParm("strOrderSid", ParameterDirection.Input, EDbType.String, 36, strOrderSid));
            parmCollection.Add(new CDbParm("strPayMode", ParameterDirection.Input, EDbType.String, 36, strPayMode)); 
            DataSet ds = this.DataObj.QueryBySP("SHP_SELECT_ORDER_INFO_FINDER", parmCollection);
            ds.Tables[0].TableName = this.TableName;
            return ds;
        }


        public void UpdateStatisticsInfo(string strOrderId)
        {
            CDbParmCollection parmCollection = new CDbParmCollection();
            parmCollection.Add(new CDbParm("strOrderId", ParameterDirection.Input, EDbType.String, 36, strOrderId));
            this.DataObj.ExecuteBySP("SHP_UPDATE_STATISTICS_INFO", parmCollection);
        }

        public DataSet SelectOrderForCustomFinder(string strDealerId, string strUser, string strGoodsNme, string strDealerNme, string strOrderNo, string strStartDt, string strEndDt, string strTradeSid, string strReviewSid, string strAfterSvcSid)
        {
            CDbParmCollection parmCollection = new CDbParmCollection();
            parmCollection.Add(new CDbParm("strGoodsNme", ParameterDirection.Input, EDbType.String, 36, strGoodsNme));
            parmCollection.Add(new CDbParm("strDealerNme", ParameterDirection.Input, EDbType.String, 36, strDealerNme));
            parmCollection.Add(new CDbParm("strOrderNo", ParameterDirection.Input, EDbType.String, 36, strOrderNo));
            parmCollection.Add(new CDbParm("strStartDt", ParameterDirection.Input, EDbType.String, 36, strStartDt));
            parmCollection.Add(new CDbParm("strEndDt", ParameterDirection.Input, EDbType.String, 36, strEndDt));
            parmCollection.Add(new CDbParm("strTradeSid", ParameterDirection.Input, EDbType.String, 36, strTradeSid));
            parmCollection.Add(new CDbParm("strReviewSid", ParameterDirection.Input, EDbType.String, 36, strReviewSid));
            parmCollection.Add(new CDbParm("strAfterSvcSid", ParameterDirection.Input, EDbType.String, 36, strAfterSvcSid));
            parmCollection.Add(new CDbParm("strDealerId", ParameterDirection.Input, EDbType.String, 36, strDealerId));
            parmCollection.Add(new CDbParm("strUser", ParameterDirection.Input, EDbType.String, 36, strUser));
            DataSet ds = this.DataObj.QueryBySP("SHP_SELECT_ORDER_FOR_CUSTOM_FINDER", parmCollection);
            ds.Tables[0].TableName = this.TableName;
            return ds;
        }

        public string GetLogisticInfo(string strOrderId)
        {
            return "";
        }

        public void CancelOrder(string strOrderId)
        {
            DataSet ds = this.SelectById(strOrderId);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["USER_NME"].ToString().ToUpper() != msBsc.SvRef.UserInfo.Operator.ToUpper())
                {
                    throw new Exception("非法的操作，只能对自己购买的订单执行该操作！");
                }
                if (ds.Tables[0].Rows[0]["TRADE_STATUS_TXT"].ToString() == "等待买家付款")
                {
                    ds.Tables[0].Rows[0]["TRADE_STATUS_TXT"] = "交易关闭";
                    ds.Tables[0].Rows[0]["CLOSE_DT"] = DateTime.Now;
                    ds.Tables[0].Rows[0]["ORDER_CLOSE_DESC"] = "买家主动取消订单";
                    this.Update(ds);
                }
                else
                {
                    throw new Exception("订单处于" + ds.Tables[0].Rows[0]["TRADE_STATUS_TXT"].ToString() + "状态，不能被取消！");
                }
            }
        }

        public void TipSellerDeli(string strOrderId)
        {
            DataSet ds = this.SelectById(strOrderId);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["USER_NME"].ToString().ToUpper() != msBsc.SvRef.UserInfo.Operator.ToUpper())
                {
                    throw new Exception("非法的操作，只能对自己购买的订单执行该操作！");
                }
                if (ds.Tables[0].Rows[0]["TRADE_STATUS_TXT"].ToString() == "买家已付款")
                { 

                } 
            }
        }


        public void ReturnCharge(string strOrderId)
        {
            DataSet ds = this.SelectById(strOrderId);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["USER_NME"].ToString().ToUpper() != msBsc.SvRef.UserInfo.Operator.ToUpper())
                {
                    throw new Exception("非法的操作，只能对自己购买的订单执行该操作！");
                }
                if (ds.Tables[0].Rows[0]["TRADE_STATUS_TXT"].ToString() == "买家已付款")
                {
                    ds.Tables[0].Rows[0]["TRADE_STATUS_TXT"] = "退款中的订单";
                    ds.Tables[0].Rows[0]["ORDER_CLOSE_DESC"] = "买家要求退款并取消订单";
                    this.Update(ds);
                }
                else
                {
                    throw new Exception("订单处于" + ds.Tables[0].Rows[0]["TRADE_STATUS_TXT"].ToString() + "状态，不能申请退款！");
                }
            }
        }

        public void SetLongAutoReceiptDt(string strOrderId)
        {
            DataSet ds = this.SelectById(strOrderId);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["USER_NME"].ToString().ToUpper() != msBsc.SvRef.UserInfo.Operator.ToUpper())
                {
                    throw new Exception("非法的操作，只能对自己购买的订单执行该操作！");
                }
                if (ds.Tables[0].Rows[0]["TRADE_STATUS_TXT"].ToString() == "卖家已发货")
                {
                    ds.Tables[0].Rows[0]["AUTO_AFFIRM_DT"] = ((DateTime)ds.Tables[0].Rows[0]["DELIVER_DT"]).AddDays(12); 
                    this.Update(ds);
                }
                else
                {
                    throw new Exception("订单处于" + ds.Tables[0].Rows[0]["TRADE_STATUS_TXT"].ToString() + "状态，不能延长收货时间！");
                }
            }
        }

        public void AddNewBuyerMsg(string strOrderId,string strNewMsg)
        {
            DataSet ds = this.SelectById(strOrderId);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["USER_NME"].ToString().ToUpper() != msBsc.SvRef.UserInfo.Operator.ToUpper())
                {
                    throw new Exception("非法的操作，只能对自己购买的订单执行该操作！"); 
                }
                if (ds.Tables[0].Rows[0]["TRADE_STATUS_TXT"].ToString() == "等待买家付款")
                {
                    ds.Tables[0].Rows[0]["BUY_MEMO"] =ds.Tables[0].Rows[0]["BUY_MEMO"].ToString()+" "+ strNewMsg; 
                    this.Update(ds);
                }
                else
                {
                    throw new Exception("订单处于" + ds.Tables[0].Rows[0]["TRADE_STATUS_TXT"].ToString() + "状态，不能增加留言信息！");
                }
            }
        }

        public void DeleteOrder(string strOrderId)
        {
            DataSet ds = this.SelectById(strOrderId);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["USER_NME"].ToString().ToUpper() != msBsc.SvRef.UserInfo.Operator.ToUpper())
                {
                    throw new Exception("非法的操作，只能对自己购买的订单执行该操作！");
                }
                if (ds.Tables[0].Rows[0]["TRADE_STATUS_TXT"].ToString() == "交易成功" || ds.Tables[0].Rows[0]["TRADE_STATUS_TXT"].ToString() == "交易关闭")
                {
                    ds.Tables[0].Rows[0]["BUYER_DEL"] = true;
                    if (ds.Tables[0].Rows[0]["TRADE_STATUS_TXT"].ToString() == "交易关闭")
                    {
                        if (ds.Tables[0].Rows[0]["SELLER_DEL"].ToString() == "True")
                        {
                            ds.Tables[0].Rows[0].AcceptChanges();
                            ds.Tables[0].Rows[0].Delete();
                        }
                    }
                    this.Update(ds);
                }
                else
                {
                    throw new Exception("订单处于" + ds.Tables[0].Rows[0]["TRADE_STATUS_TXT"].ToString() + "状态，不能删除！");
                }
            }
        }

        public void ShopCancelOrder(string strOrderId)
        {
            DataSet ds = this.SelectById(strOrderId);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["COMM_USER_NME"].ToString().ToUpper() != msBsc.SvRef.UserInfo.Operator.ToUpper())
                {
                    throw new Exception("非法的操作，只能对自己出售的订单执行该操作！");
                }
                if (ds.Tables[0].Rows[0]["TRADE_STATUS_TXT"].ToString() == "等待买家付款")
                {
                    ds.Tables[0].Rows[0]["TRADE_STATUS_TXT"] = "交易关闭";
                    ds.Tables[0].Rows[0]["CLOSE_DT"] = DateTime.Now;
                    ds.Tables[0].Rows[0]["ORDER_CLOSE_DESC"] = "卖家取消订单";
                    this.Update(ds);
                }
                else
                {
                    throw new Exception("订单处于" + ds.Tables[0].Rows[0]["TRADE_STATUS_TXT"].ToString() + "状态，不能被取消！");
                }
            }
        }

        public void ShopReturnCharge(string strOrderId)
        {
            DataSet ds = this.SelectById(strOrderId);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["COMM_USER_NME"].ToString().ToUpper() != msBsc.SvRef.UserInfo.Operator.ToUpper())
                {
                    throw new Exception("非法的操作，只能对自己出售的订单执行该操作！");
                }
                if (ds.Tables[0].Rows[0]["TRADE_STATUS_TXT"].ToString() == "退款中的订单")
                {
                    ds.Tables[0].Rows[0]["TRADE_STATUS_TXT"] = "交易关闭";
                    ds.Tables[0].Rows[0]["CLOSE_DT"] = DateTime.Now;
                    ds.Tables[0].Rows[0]["ORDER_CLOSE_DESC"] = "买家申请退款并取消订单，卖家已同意";
                    this.Update(ds);
                }
                else if (ds.Tables[0].Rows[0]["TRADE_STATUS_TXT"].ToString() == "买家已付款")
                {
                    ds.Tables[0].Rows[0]["TRADE_STATUS_TXT"] = "交易关闭";
                    ds.Tables[0].Rows[0]["CLOSE_DT"] = DateTime.Now;
                    ds.Tables[0].Rows[0]["ORDER_CLOSE_DESC"] = "卖家主动撤销订单并退款";
                    this.Update(ds);
                }
                else
                {
                    throw new Exception("订单处于" + ds.Tables[0].Rows[0]["TRADE_STATUS_TXT"].ToString() + "状态，不能被取消！");
                }
            }
        }

        public void SetDiscountGoodsAmt(string strOrderId,string strListId,string strAmt)
        {
            DataSet ds = this.SelectById(strOrderId);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["COMM_USER_NME"].ToString().ToUpper() != msBsc.SvRef.UserInfo.Operator.ToUpper())
                {
                    throw new Exception("非法的操作，只能对自己出售的订单执行该操作！");
                }
                if (ds.Tables[0].Rows[0]["TRADE_STATUS_TXT"].ToString() == "等待买家付款")
                {
                    using (msBsc.AppLogic.DataAlc.CBizShpOrderFormList objlist = new CBizShpOrderFormList())
                    {
                        DataSet dslist = objlist.SelectById(strListId);
                        if (dslist.Tables[0].Rows.Count <= 0)
                        {
                            throw new Exception("未找到当前订单下指定要优惠的商品明细！");
                        }
                        else
                        {
                            decimal decamt = decimal.Parse(strAmt == string.Empty ? "0.00" : strAmt);

                            if (decamt <0)
                            {
                                throw new Exception("优惠金额不能为负数，不能完成操作！");
                            }
                            if (decamt > (int)dslist.Tables[0].Rows[0]["GOODS_QTY"] * (decimal)dslist.Tables[0].Rows[0]["GOODS_STRIKE_PRICE"])
                            {
                                throw new Exception("优惠金额超过商品销售金额，不能完成操作！");
                            }

                            dslist.Tables[0].Rows[0]["DISCOUNT_AMT"] = decamt;
                            objlist.Update(dslist);
                        }
                    }
                    this.DataObj.ExecuteBySQL(@"
    UPDATE A SET 
    DISCOUNT_AMT=ISNULL(V1.DISCOUNT_AMT,0)
    ,GOODS_AMT=ISNULL(V1.GOODS_AMT,0)-ISNULL(V1.DISCOUNT_AMT,0)
    ,PAYABLE_AMT= (ISNULL(V1.GOODS_AMT,0)-ISNULL(V1.DISCOUNT_AMT,0))+ISNULL(A.LOGISTICS_AMT,0)
    ,PAID_AMT= ((ISNULL(V1.GOODS_AMT,0)-ISNULL(V1.DISCOUNT_AMT,0))+ISNULL(A.LOGISTICS_AMT,0))-ISNULL(A.PAID_SCORE_AMT,0)
    FROM SHP_ORDER_FORM A LEFT OUTER JOIN (SELECT B.ORDER_FORM_ID,DISCOUNT_AMT=SUM(B.DISCOUNT_AMT),GOODS_AMT=SUM(B.GOODS_QTY*B.GOODS_STRIKE_PRICE) FROM SHP_ORDER_FORM_LIST B WHERE B.ORDER_FORM_ID='" + strOrderId + @"' GROUP BY B.ORDER_FORM_ID)  AS V1 ON A.ORDER_FORM_ID=V1.ORDER_FORM_ID WHERE A.ORDER_FORM_ID='" + strOrderId + @"'
    ");
                    this.DataObj.ExecuteBySQL("UPDATE B SET TRADING_AMT=A.PAID_AMT FROM SHP_ORDER_FORM A LEFT OUTER JOIN SHP_CHARGE_TRADING_LOG B ON A.ORDER_FORM_NO=B.TRADING_NO AND B.TRADING_SID='待支付' AND B.PAY_DIR='支出' WHERE A.ORDER_FORM_ID='" + strOrderId + "'");
                } 
                else
                {
                    throw new Exception("订单处于" + ds.Tables[0].Rows[0]["TRADE_STATUS_TXT"].ToString() + "状态，不能更改优惠金额！");
                }
            }
        }

        public void SetLogisticsAmt(string strOrderId, string strAmt)
        {
            DataSet ds = this.SelectById(strOrderId);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["COMM_USER_NME"].ToString().ToUpper() != msBsc.SvRef.UserInfo.Operator.ToUpper())
                {
                    throw new Exception("非法的操作，只能对自己出售的订单执行该操作！");
                }
                if (ds.Tables[0].Rows[0]["TRADE_STATUS_TXT"].ToString() == "等待买家付款")
                {
                    decimal decamt = decimal.Parse(strAmt == string.Empty ? "0.00" : strAmt);

                    if (decamt < 0)
                    {
                        throw new Exception("运费不能为负数，不能完成操作！");
                    } 


                    ds.Tables[0].Rows[0]["LOGISTICS_AMT"] = decamt;


                    ds.Tables[0].Rows[0]["PAYABLE_AMT"] = (decimal)ds.Tables[0].Rows[0]["GOODS_AMT"] + (decimal)ds.Tables[0].Rows[0]["LOGISTICS_AMT"];
                    ds.Tables[0].Rows[0]["PAID_AMT"] = ((decimal)ds.Tables[0].Rows[0]["GOODS_AMT"] + (decimal)ds.Tables[0].Rows[0]["LOGISTICS_AMT"]) - (decimal)ds.Tables[0].Rows[0]["PAID_SCORE_AMT"]; 


                    this.Update(ds);

                    this.DataObj.ExecuteBySQL("UPDATE B SET TRADING_AMT=A.PAID_AMT FROM SHP_ORDER_FORM A LEFT OUTER JOIN SHP_CHARGE_TRADING_LOG B ON A.ORDER_FORM_NO=B.TRADING_NO AND B.TRADING_SID='待支付' AND B.PAY_DIR='支出' WHERE A.ORDER_FORM_ID='" + strOrderId + "'");
                } 
                else
                {
                    throw new Exception("订单处于" + ds.Tables[0].Rows[0]["TRADE_STATUS_TXT"].ToString() + "状态，不能更改快递费用金额！");
                }
            }
        }
        public void ShopDeleteOrder(string strOrderId)
        {
            DataSet ds = this.SelectById(strOrderId);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["COMM_USER_NME"].ToString().ToUpper() != msBsc.SvRef.UserInfo.Operator.ToUpper())
                {
                    throw new Exception("非法的操作，只能对自己出售的订单执行该操作！");
                }
                if (ds.Tables[0].Rows[0]["TRADE_STATUS_TXT"].ToString() == "交易成功" || ds.Tables[0].Rows[0]["TRADE_STATUS_TXT"].ToString() == "交易关闭")
                {
                    ds.Tables[0].Rows[0]["SELLER_DEL"] = true;
                    if (ds.Tables[0].Rows[0]["TRADE_STATUS_TXT"].ToString() == "交易关闭")
                    {
                        if (ds.Tables[0].Rows[0]["BUYER_DEL"].ToString() == "True")
                        {
                            ds.Tables[0].Rows[0].AcceptChanges();
                            ds.Tables[0].Rows[0].Delete();
                        }
                    }
                    this.Update(ds);
                } 
                else
                {
                    throw new Exception("订单处于" + ds.Tables[0].Rows[0]["TRADE_STATUS_TXT"].ToString() + "状态，不能删除！");
                }
            }
        }
    }
}
